﻿package{
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.TextField;
	import flash.net.SharedObject;
	import flash.system.System;
	
	import flash.media.Sound;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	
	
	public class Clickr extends MovieClip{
		
		
		private var playScreen:Menu_InGame;
		private var menuTitle:Menu_Title;
		private var menuRecords:Menu_Records;
		private var menuGetClickr:Menu_GetClickr;
		private var menuHelp:Menu_Help;
		private var menuResult:Menu_Result;
		private var menuPause:Menu_Pause;
		private var menuBoard:Menu_LeaderBoard;
		private var gameLevel:Sprite;
		private var popupLevel:Sprite;
		private var pauseIn;
		
		public var cursor:GUI_Cursor;
		public static var bestScore:Number;
		public static var bestScored:Boolean;
		public static var cubeArray:Array;
		public static var lastNickName:String;
		
		private var InfoTF:TextField;
		
		
		
		public static var snd_Click;
		public static var snd_Rotate;
		public static var snd_BigStone;
		public static var snd_MagicTurn;
		public static var snd_EraseBlock;
		public static var snd_EraseBlockUse;
		public static var snd_ClickSmallStar;
		public static var snd_AllClear;
		public static var snd_BigStar;
		public static var snd_BigStarLast;
		public static var snd_DoubleChance;
		public static var snd_Double;
		public static var snd_Clickloss;
		public static var snd_PC_AllClearN;
		public static var snd_PC_AllClearNN;
		
		public static var snd_Result_Normal;
		public static var snd_Result_HighScore;
		public static var snd_Alert50;
		public static var snd_Alert57;
		public static var snd_PlayEnd;
		public static var snd_PointUp1;
		public static var snd_PointUp2;
		public static var snd_PointUp3;
		
		
		public function Clickr(){
			
			
			addEventListener(Event.ADDED_TO_STAGE, Init);
			
		}
		
		private function Init(e:Event) {
			removeEventListener(Event.ADDED_TO_STAGE, Init);
			stop();
			
			gameLevel = new Sprite();
			addChild(gameLevel);
			
			popupLevel = new Sprite();
			
			
			addChild(popupLevel);
			
			
			cursor = new GUI_Cursor();
			addChild(cursor);
			
			//각 메뉴 이동 관련 이벤트에 대해 리스너 등록
			addEventListener(NavigationEvent.ANI_END, aniEndHandler, true);
			addEventListener(NavigationEvent.CLOSE, closeHandler, true);
			
			addEventListener(NavigationEvent.REPLAY, onRequestReplay, true);
			
			
			addEventListener(NavigationEvent.SHOW_LEADERBOARD, onRequestLeaderboard, true);
			
			//menuTitle - capture phase
			addEventListener(NavigationEvent.START, onRequestStart, true);
			addEventListener(NavigationEvent.SHOW_HELP, onRequestHelp, true);
			addEventListener(NavigationEvent.SHOW_RECORD, onRequestRecord, true);
			addEventListener(NavigationEvent.SHOW_GETCLICKR, onRequestGetClickr, true);
			
			//playScreen - capture phase
			addEventListener(NavigationEvent.QUIT, onRequestQuit, true);
			addEventListener(NavigationEvent.PAUSE, onRequestPause, true);
			addEventListener(NavigationEvent.REQUEST_QUIT, onRequestRequestQuit, true);
			addEventListener(NavigationEvent.RESTART, onRequestRestart, true);
			addEventListener(NavigationEvent.SHOW_RESULT, onRequestResult, true);
			addEventListener(NavigationEvent.SHOW_RESULT_HIGH, onRequestResultHigh, true);
			
			//타이틀화면 보여주기
			var titleIn = new Menu_Title_in();
			gameLevel.addChild(titleIn);
			
			
			
			snd_Click = new Click();
			snd_Rotate = new Rotate();
			snd_BigStone = new BigStone();
			snd_MagicTurn = new MagicTurn();
			snd_EraseBlock = new PC_EraseBlock();
			snd_EraseBlockUse = new PC_EraseBlockUse();
			snd_ClickSmallStar = new ClickSmallStar();
			snd_AllClear = new AllClear();
			snd_BigStar = new BigStar();
			snd_BigStarLast = new BigStarLast();
			snd_DoubleChance = new PC_DoubleChance();
			snd_Double = new Double();
			snd_Clickloss = new Clickloss();
			snd_PC_AllClearN = new PC_AllClearN();
			snd_PC_AllClearNN = new PC_AllClearNN();
			
			snd_Result_Normal = new Result_Normal();
			snd_Result_HighScore = new Result_HighScore();
			snd_Alert50 = new PC_Alert50();
			snd_Alert57 = new PC_Alert57();
			snd_PlayEnd = new PC_PlayEnd();	
			snd_PointUp1 = new PC_PointUp1();
			snd_PointUp2 = new PC_PointUp2();
			snd_PointUp3 = new PC_PointUp3();
			
		}
		
		
		private function aniEndHandler(e:NavigationEvent):void {
			
			trace(this, ": ", e.gotoMenu, "으로 가기");
			if (e.gotoMenu != null) {
				
				switch(e.gotoMenu) {
					case "Menu_Title_in":
					var titleIn = new Menu_Title_in();
					gameLevel.addChild(titleIn);
					break;				
					case "Menu_Title":
					menuTitle = new Menu_Title();
					gameLevel.addChild(menuTitle);
					break;
					case "Menu_InGame":
					
					playScreen = new Menu_InGame();
					gameLevel.addChild(playScreen);
					if(menuTitle != null){
						if(gameLevel.contains(menuTitle)){
							gameLevel.removeChild(menuTitle);
						}
					}
					menuTitle = null;
					break;
					case "Menu_Result":
					playScreen.dispose();	//강한 결합
					
					gameLevel.removeChild(playScreen);
					playScreen = null;
					
					menuResult = new Menu_Result();
					gameLevel.addChild(menuResult);
					break;
					case "Menu_LeaderBoard":
					menuBoard = new Menu_LeaderBoard();
					popupLevel.addChild(menuBoard);
					break;
					case "Menu_Help":
					menuHelp = new Menu_Help();
					popupLevel.addChild(menuHelp);
					break;
					case "Menu_Records":
					
					menuRecords = new Menu_Records();
					popupLevel.addChild(menuRecords);
					break;
					case "Menu_GetClickr":
					menuGetClickr = new Menu_GetClickr();
					popupLevel.addChild(menuGetClickr);
					break;
					
					case "Menu_Pause":
					menuPause = new Menu_Pause();
					popupLevel.addChild(menuPause);
					break;
				}
			}
			
			//이벤트를 전달한 객체 삭제.
			e.caller.parent.removeChild(e.caller);
			
			
			//e.gotoMenu를 보여줌
			
			
		}
		
		private function closeHandler(e:NavigationEvent):void {
			e.caller.parent.removeChild(e.caller);
			
			switch(e.gotoMenu) {
				case "help":
				var helpOut = new Menu_Help_out();
				popupLevel.addChild(helpOut);
				break;
				case "records":
				var recordsOut = new Menu_Records_out();
				popupLevel.addChild(recordsOut);
				break;
				case "getClickr":
				var getClickrOut = new Menu_GetClickr_out();
				popupLevel.addChild(getClickrOut);
				break;
				case "resultOut":
				var resultOut = new Menu_Result_out();
				popupLevel.addChild(resultOut);
				break;
				case "pause":
				var pauseOut = new Menu_Pause_out();
				popupLevel.addChild(pauseOut);
				break;
				case "board":
				var boardOut = new Menu_LeaderBoard_out();
				popupLevel.addChild(boardOut);
				break;
			}
			
			
		}
		
		public function onRequestStart(e:NavigationEvent):void{
			//여기서 메인 메뉴는 지워줘야 안되나?
			
			var inGameIn:Menu_InGame_in = new Menu_InGame_in();
			gameLevel.addChild(inGameIn);
			
		}
		public function onRequestReplay(e:NavigationEvent):void {
			trace("onReqeustReplay Got", this);
			
			menuResult.removeEventListener(NavigationEvent.CLOSE, closeHandler);
			gameLevel.removeChild(menuResult);
			menuResult = null;
			
			var inGameIn:Menu_InGame_in = new Menu_InGame_in();
			gameLevel.addChild(inGameIn);
			
		}
		
		private function onRequestHelp(e:NavigationEvent):void {
			var helpIn = new Menu_Help_in();
			popupLevel.addChild(helpIn);
		}
		private function onRequestRecord(e:NavigationEvent):void {
			trace("onRequestRecord called");
			
			var recordsIn = new Menu_Records_in();
			popupLevel.addChild(recordsIn);
		}
		
		private function onRequestLeaderboard(e:NavigationEvent):void {
			trace("onRequestLeaderboard called");
			var boardIn = new Menu_LeaderBoard_in();
			popupLevel.addChild(boardIn);
			
		}
		
		private function onRequestGetClickr(e:NavigationEvent):void {
			var getClickrIn = new Menu_GetClickr_in();
			popupLevel.addChild(getClickrIn);
		}
		
		private function onRequestResult(e:NavigationEvent):void {
			trace("onRequestResult called", e.target);
			var resultIn = new Menu_Result_in();
			gameLevel.addChild(resultIn);
			
		}
		private function onRequestResultHigh(e:NavigationEvent):void {
			trace("onRequestResultHigh called", e.target);
			var resultIn = new Menu_Result_in_highscore();
			gameLevel.addChild(resultIn);
			
		}
		
		public function onRequestQuit(e:NavigationEvent = null):void{
			trace("onRequestQuit called");
			
			popupLevel.removeChild(menuPause);
			menuPause = null;
			
			
			playScreen.dispose();
			
			gameLevel.removeChild(playScreen);
			playScreen = null;
			
			var titleIn = new Menu_Title_in();
			//menuTitle = new Menu_Title();
			
			gameLevel.addChild(titleIn);
		}
		
		public function onRequestPause(e:NavigationEvent):void {
			trace("Clickr onRequestPause called");
			if (Menu_InGame.isPaused) {
				trace("but returned");
				//ESC를 눌렀을 때 Menu_Pause창이 있으면 resume
				if(Menu_Pause.menuPause){
					Menu_Pause.menuPause.onClickResume();
				}
				
				
				//closeHandler(new NavigationEvent(
				//dispatchEvent(new NavigationEvent(NavigationEvent.CLOSE, "pause", this));
				
				
				return;
			}
			
			
			Menu_InGame.isPaused = true;
			
			pauseIn = new Menu_Pause_in();
			popupLevel.addChild(pauseIn);
			
		}
		
		public function onRequestRequestQuit(e:NavigationEvent):void {
			
			trace("Clickr onRequestRequestQuit called");
			if (Menu_Pause.menuPause && Menu_InGame.isPaused) {	//pauseMenu가 있으면
			
				Menu_Pause.menuPause.onClickQuit();
				//onRequestQuit();
				
			}else {
				
				trace("ignore quit request");
				
			}
		}
		
		public function onRequestRestart(e:NavigationEvent):void{
			trace("Restart the Fuck");
			if (Menu_InGame.isPaused) {
				trace("but returned");
				return;
			}	
			
			restartGame();
			
		}
		
		
		public function restartGame():void {
			
			playScreen.dispose();
			gameLevel.removeChild(playScreen);
			playScreen = null;
			
			playScreen = new Menu_InGame();
			
			gameLevel.addChild(playScreen);
		}
		
		public static function makeCurrency(_num:Number):String {
			var str:String = Math.floor(_num).toString();
			
			var arr:Array = str.split("");
			var len:uint = arr.length;
		 
			for(var i:int=len-1, cnt:int=1; i>0; i--, cnt++){
				if((cnt % 3)==0){
					arr[i] = "," + arr[i];
				}
			}
			str = arr.join("");
			return str;
		}
		
		public static function toggleSound(_isSoundOn:Boolean) {
			trace("toggleSound called");
			var transform:SoundTransform = SoundMixer.soundTransform;
			if (_isSoundOn) {
				transform.volume = 1;
			}else {
				transform.volume = 0;
			}
			SoundMixer.soundTransform = transform;
		}
		
	}
	
	
}